Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next

Manipulating Heap Zones

The Memory Manager provides several routines for initializing and resizing heap zones.

To obtain information about the current application partition, applications can call the GetApplLimit function and the TopMem function. If your application uses the stack extensively, you might want to ensure that the stack is set to at least some minimum size, at the expense of the heap. To do so, use the SetApplLimit procedure to change the application heap limit before you call the MaxApplZone procedure.

To initialize a new heap zone, use the InitZone procedure. The Operating System automatically initializes the application zone by calling the SetApplBase procedure, which subsequently calls the InitApplZone procedure.

GetApplLimit

Use the GetApplLimit function to get the application heap limit, beyond which the application heap cannot expand.

FUNCTION GetApplLimit: Ptr;

DESCRIPTION

The GetApplLimit function returns the current application heap limit. The Memory Manager expands the application heap only up to the byte preceding this limit.

Nothing prevents the stack from growing below the application limit. If the Operating System detects that the stack has crashed into the heap, it generates a system error. To avoid this, use GetApplLimit and the SetApplLimit procedure to set the application limit low enough so that a growing stack does not encounter the heap.

The GetApplLimit function does not indicate the amount of memory available to your application.

ASSEMBLY-LANGUAGE INFORMATION

The global variable ApplLimit contains the current application heap limit.

SetApplLimit

Use the SetApplLimit procedure to set the application heap limit, beyond which the application heap cannot expand.

PROCEDURE SetApplLimit (zoneLimit: Ptr);
zoneLimit
A pointer to a byte in memory demarcating the upper boundary of the application heap zone. The zone can grow to include the byte preceding zoneLimit in memory, but no further.

DESCRIPTION

The SetApplLimit procedure sets the current application heap limit to zoneLimit . The Memory Manager then can expand the application heap only up to the byte preceding the application limit. If the zone already extends beyond the specified limit, the Memory Manager does not cut it back but does prevent it from growing further.

The zoneLimit parameter is not a byte count, but an absolute byte in memory. Thus, you should use the SetApplLimit procedure only with a value obtained from the Memory Manager functions GetApplLimit or ApplicationZone .

You cannot change the limit of zones other than the application heap zone.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for SetApplLimit are

Registers on entry

A0

Pointer to desired new zone limit

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

TopMem

To find out the location of the top of an application's partition, you can use the TopMem function, which exhibits special behavior during the startup process.

FUNCTION TopMem: Ptr;

DESCRIPTION

Except during the startup process, the TopMem function returns a pointer to the byte at the top of an application's partition, directly above the jump table. The function does this to maintain compatibility with programs that check TopMem to find out how much memory is installed in a computer. To obtain this information, you can currently use the Gestalt function.

The function exhibits special behavior at startup time, and the value it returns controls the amount by which an extension can lower the value of the global variable BufPtr at startup time. If you are writing a system extension, you should not lower the value of BufPtr by more than MemTop DIV 2 + 1024 . If you do lower BufPtr too far, the startup process generates an out-of-memory system error.

You should never need to call TopMem except during the startup process.

ASSEMBLY-LANGUAGE INFORMATION

The TopMem function returns the value of the MemTop global variable.

InitZone

If you want to use heap zones other than the original application heap zone, a temporary memory zone, or the system heap zone, you can use the InitZone procedure to initialize a new heap zone.

PROCEDURE InitZone (pGrowZone: ProcPtr; cMoreMasters: Integer;
                                         limitPtr, startPtr: Ptr);
pGrowZone
A pointer to a grow-zone function for the new heap zone. If you do not want the new zone to have a grow-zone function, set this parameter to NIL .
cMoreMasters
The number of master pointers that should be allocated at a time for the new zone. The Memory Manager allocates this number initially, and, if it needs to allocate more later, allocates them in increments of this same number.
limitPtr
The first byte beyond the end of the zone.
startPtr
The first byte of the new zone.

DESCRIPTION

The InitZone procedure creates a new heap zone, initializes its header and trailer, and makes it the current zone. Although the new zone occupies memory addresses from startPtr through limitPtr-1 , the new zone includes a zone header and a zone trailer. In addition, the new zone contains a block header for the master pointer block and 4 bytes for each master pointer. If you need to create a zone with some specific number of usable bytes, see "Organization of Memory," beginning on Organization of Memory , for details on the sizes of the zone header, zone trailer, and block header.

The sizes of zones and block headers may change in future system software versions. You should ensure that your zones are large enough to accommodate a reasonable increase in the sizes of those structures.

SPECIAL CONSIDERATIONS

Because InitZone changes the current zone, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for InitZone are

Registers on entry

A0

Pointer to parameter block

Registers on exit

D0

Result code

The parameter block whose address is passed in register A0 has no Pascal type definition. It has this structure:

 

Æ

startPtr

Ptr

The first byte of the new zone.

Æ

limitPtr

Ptr

The first byte beyond the new zone.

Æ

cMoreMasters

Integer

The number of master pointers to be allocated at a time.

Æ

pGrowZone

ProcPtr

A pointer to the new zone's grow-zone function, or NIL if none.

RESULT CODES

noErr

0

No error

InitApplZone

The Process Manager calls the InitApplZone procedure indirectly when it starts up your application. You should never need to call it. It is documented for completeness only.

PROCEDURE InitApplZone;

DESCRIPTION

The InitApplZone procedure initializes the application heap zone and makes it the current zone. The Memory Manager discards the contents of any previous application zone and discards all previously existing blocks in that zone. The procedure sets the zone's grow-zone function to NIL .

Reinitializing the application zone from within a running program is dangerous, because the application's code itself normally resides in the application zone. To do so safely, you must make sure that the code containing the InitApplZone call is not in the application zone.

SPECIAL CONSIDERATIONS

You should not call InitApplZone at all, but, if you must, be sure not to call it at interrupt time because it could purge and allocate memory.

ASSEMBLY-LANGUAGE INFORMATION

The registers on exit for InitApplZone are

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error

SetApplBase

The Process Manager calls the SetApplBase procedure when it starts up your application. You should never need to call it. It is documented for completeness only.

PROCEDURE SetApplBase (startPtr: Ptr);
startPtr
The starting address for the application heap zone to be initialized.

DESCRIPTION

The SetApplBase procedure sets the starting address of the application heap zone for the application being initialized to the address designated by startPtr , and then calls the InitApplZone procedure.

Like InitApplZone , SetApplBase is a potentially dangerous operation, because the program's code itself normally resides in the application heap zone. To do so safely, you must make sure that the code containing the SetApplBase call is not in the application zone.

SPECIAL CONSIDERATIONS

You should not call SetApplBase at all, but, if you must, be sure not to call it at interrupt time because it affects memory.

ASSEMBLY-LANGUAGE INFORMATION

The registers on exit for SetApplBase are

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next